'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'], function ($) {
  return {
    
    init: function () {
      this.initPage();
      this.initChangeParent();
      this.initChangeBranch();
      this.initChangeLevel();
      this.initSyncShortname();
      this.initDelete();
    },

    initPage: function () {
      
    	var that = this;
    	
      $(document).on("shown.bs.modal", "#introduce", function (e) {
        
      	$('.create-panel').hide();
      	$('#company').hide();
      	
        $('select[name="level"]').click(function(e){
          
          var code = $(e.currentTarget).val();
          
          //如果选择的是一级供应商
          if(code.indexOf("FIRST") == 0){
            
          	$('#company').hide();
          	$('select[name="parentId"]').data('validate', '');
          	
          } else { //如果选择的是二级供应商
            
          	$('#company').show();
          	$('select[name="parentId"]').data('validate', 'required');
          	
          }
          
        });
      	
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        var count = 0;
        $('#code-search').on("click", function(e){
          $("select.form-control").trigger("blur");
           var s = $('#tip').text().length;
           
          if($('#tip').text().length>0){
        	  $('#tip').text('')
          }
          
          if($("#level").length > 0){
        	  $('.create-panel').hide();
          	 $('#company').hide();
          }
          that._getAjaxData();
         })
         
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
      
    },
    
    _getAjaxData: function() {
      
      var code = $('input[name="code"]').val().trim();
      var type = $('select[name="type"]').val().trim();
      if(code != '' && type != '') {
      
        $.ajax({
          url:window.__CTX__.root + "/suppliers?prepare=true&code="+code+"&type="+type,
          type:"GET",
        }).done(function(data){
          
          data = typeof data === 'String' ? JSON.parse(data) : data
          if(parseInt(data.code) == 0) {  
            
            var response = data.data;
            if(response.supplierSources.length == 1) {
            	
              $('.create-panel').show();
              
              $.each(response.supplierSources, function(key, val){
              	var id = response.supplierSources[key].id;
              	var code = response.supplierSources[key].code;
              	var name = response.supplierSources[key].name;
              	var shortName = response.supplierSources[key].shortName;
              	$('#supplierCode').text(code)
              	$('#supplierName').text(name)
              	
              	$('input[name="orgId"]').val(id);
              	$('input[name="code"]').val(code);
              	$('input[name="name"]').val(name);
              	$('input[name="shortName"]').val(shortName);
              	$('input[name="type"]').val(type);
              	
              });
              
            } else if(response.supplierSources.length == 0) {//如果没有可引入的供方
            	$('#tip').text('有0条数据')
            } else{
            	$('#tip').text('数据错误')
            }
  
          } else if(parseInt(data.code) == 1){
          	$('#tip').text(data.message)
          } else if(parseInt(data.code) == -1){
          	$('#tip').text(data.message)
          }
          
        }).fail(function() {
          console.log(data.code);
  //        changeBackdropMessage('查询失败', 'text-danger', data.url)
        })
        $("form button").removeClass("disabled")
      } else {
      	$('#tip').text('请填写服务供方代码或选择服务供方类型！');
      }
      
    },
    
    initChangeParent: function() {
      
      $(document).on("shown.bs.modal", '#change-parent', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });

    },
    
    initChangeBranch: function() {
      
      $(document).on("shown.bs.modal", '#change-branch', function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
      
    },
    
    initChangeLevel: function() {
      
      $(document).on("shown.bs.modal", '#change-level', function(e){
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
      
    },
    
    initSyncShortname: function() {
      $(document).on("shown.bs.modal", '#sync-shortname', function(e){
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
    },
    
    initDelete: function() {
      $(document).on("shown.bs.modal", '#delete', function(e){
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
    }
    
  }
})

